package com.situ.sal.controller;

import com.situ.sal.entity.SalChance;
import com.situ.util.entity.CstCustomer;
import com.situ.util.entity.CstLinkman;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import com.situ.sal.service.SalPlanService;
import com.situ.sal.entity.SalPlan;
import com.situ.util.*;
import org.springframework.web.multipart.MultipartFile;

import java.beans.PropertyEditor;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * SalPlan的路由接口服务
 * 
 * @author 
 *
 */
@RestController
public class SalPlanController {

	/** SalPlanService服务 */
	@Autowired
	private SalPlanService salPlanService;
	
	/**
	 * 查询所有SalPlan数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "querySalPlan", produces = {"application/json;charset=UTF-8"})
	public ResponseData find(SalPlan value) {

		return ResponseData.ok().putDataValue("list",salPlanService.find(value));
	}
	
	/**
	 * 通过id查询SalPlan数据的方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "querySalPlanById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData findOne(@PathVariable(name="id") Integer id) {
		 return ResponseData.ok().putDataValue("obj",salPlanService.findOne(id));
	}
	
	/**
	 * 插入SalPlan属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "saveSalPlan")
	public ResponseData save(SalPlan value) {
		return ResponseData.ok().putDataValue("obj", salPlanService.saveNotNull(value));
	}
	
	/**
	 * 更新SalPlan属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateSalPlan", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(SalPlan value) {
		return ResponseData.ok().putDataValue("obj",salPlanService.updateNotNullById(value));
	}

	/**
	 * 通过id删除SalPlan数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delSalPlanById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delete(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",salPlanService.deleteById(id));
	}

	//添加计划
	@RequestMapping("/addSalPane")
	public ResponseData addSalPane(SalPlan s) {

		salPlanService.addSalPlan(s);

		return  ResponseData.ok();
	}

	//根据id查询计划
	@RequestMapping("/queryBySpId/{id}")
	public ResponseData queryBySpId(@PathVariable int id) {

		List<SalPlan> sp=salPlanService.queryBySpId(id);

		return  ResponseData.ok().putDataValue("sp", sp);
	}

	//制定
	@RequestMapping("/updateSpl")
	public ResponseData updateSpl(SalPlan s) {

		salPlanService.updateSalP(s);

		return  ResponseData.ok();
	}

	//执行计划
	@RequestMapping("/updateSp")
	public ResponseData updatSp(SalPlan s) {

		salPlanService.updatePlaResult(s);

		return  ResponseData.ok();
	}

	//开发成功
	@RequestMapping("/updateSpCg")
	public ResponseData updateSpCg(MultipartFile files, SalChance s,CstCustomer cc,CstLinkman c) {
		s.setChc_status("3");
		salPlanService.updateSpZt(s);

//		String file1FileName = files.getOriginalFilename();
//		//使用当前时间毫秒数 做为文件名称
//		file1FileName = System.currentTimeMillis() + "id" + file1FileName.substring(file1FileName.lastIndexOf("."), file1FileName.length());

//		cc.setCust_no("");
//		salPlanService.addCstcustomer(cc);
//		salPlanService.addCstLinkman(c);

		return  ResponseData.ok();
	}

	//开发失败
	@RequestMapping("/updateSpSb")
	public ResponseData updateSpSb(SalChance s) {

		s.setChc_status("4");
		salPlanService.updateSpZt(s);

		return  ResponseData.ok();
	}

	//删除
	@RequestMapping("/delSpl/{id}")
	public ResponseData delSpl(@PathVariable int id) {

		salPlanService.deleteSPById(id);

		return  ResponseData.ok();
	}

	//时间转换器
	@InitBinder  //
	public void dateconvert(WebDataBinder bind){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		PropertyEditor n=new CustomDateEditor(format, true);
		bind.registerCustomEditor(Date.class, n);
	}

}
